<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/3/4
 * Time: 17:59
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Beautydiarycate extends Base
{
    /**
     * 美丽日记分类
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $where['bdc_status'] = 1;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('beauty_diary_cate')->where($where)->count();// 获取总条数
            $lists = Db::name('beauty_diary_cate')
                ->where($where)
                ->field('bdc_id, bdc_name, bdc_is_default, bdc_time, bdc_updatetime')
                ->order('bdc_time desc')
                ->page($Nowpage,$limits)
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加美丽日记分类
     * @return mixed|\think\response\Json
     */
    public function cateAdd(){
        if(Request::instance()->isPost()){
            $bdc_name = trim(input('bdc_name'), ' ');
            $insert = [
                'bdc_name' => $bdc_name,
                'bdc_time' => date('Y-m-d H:i:s')
            ];
            $exist = Db::table('up_beauty_diary_cate')->where('bdc_status', 1)->find();
            if(!$exist){
                $insert['bdc_is_default'] = 2;
            }
            $res = Db::table('up_beauty_diary_cate')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('beautydiarycate/cateadd');
    }
    
    /**
     * 设置默认
     * @return \think\response\Json
     */
    public function setDefault(){
        $bdc_id = input('id');
        Db::startTrans();
        try{
            // 将所有分类改成非默认
            Db::table('up_beauty_diary_cate')->where('bdc_is_default', 2)->setField('bdc_is_default', 1);
            // 将该分类设置成默认
            Db::table('up_beauty_diary_cate')->where('bdc_id', $bdc_id)->setField('bdc_is_default', 2);
            Db::commit();
            return json(['code' => 200, 'msg' => '设置成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 编辑美丽日记分类
     * @return mixed|\think\response\Json
     */
    public function cateEdit(){
        $bdc_id = input('bdc_id');
        if(Request::instance()->isPost()){
            $bdc_name = trim(input('bdc_name'), ' ');
            $update = [
                'bdc_name' => $bdc_name,
                'bdc_updatetime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_beauty_diary_cate')->where('bdc_id', $bdc_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $cate = Db::table('up_beauty_diary_cate')->field('bdc_name')->where('bdc_id', $bdc_id)->find();
        return $this->fetch('beautydiarycate/cateedit', ['cate' => $cate, 'bdc_id' => $bdc_id]);
    }
    
    /**
     * 删除分类
     * @return \think\response\Json
     */
    public function cateDel(){
        $bdc_id = input('id');
        $is_default = Db::table('up_beauty_diary_cate')->where('bdc_id', $bdc_id)->value('bdc_is_default');
        if($is_default == 2) return json(['code' => 100, 'msg' => '请先切换默认分类']);
        $res = Db::table('up_beauty_diary_cate')->where('bdc_id', $bdc_id)->setField('bdc_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}
